home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / utils / graphic / viewers / general / msdos / ncsa / src / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-22  |  17.0 KB  |  640 lines

  1. /*
  2. *    parse.c
  3. *
  4. *    This set of routines takes care of the image and palette manipulations
  5. *  after an image has been read in and displayed.
  6. *
  7. *  National Center for Supercomputing Applications, University of Illinois
  8. *  153 Computing Applications Building
  9. *  605 E. Springfield Ave.
  10. *  Champaign, IL  61820     (217)244-0072
  11. *
  12. *  Quincey Koziol            August 1988
  13. *
  14. */
  15.  
  16. #include "showext.h"
  17.  
  18. /**********************************************************************
  19. *  Function    :    parse
  20. *  Purpose    :    parse the commands that options() receives and perform the actions
  21. *  Parameters    :
  22. *                c -    the character command to parse
  23. *  Returns    :    none
  24. *  Calls    :    nopal(), textmode(), grafmode(), showpic(), showpal(),
  25. *            findpal(), putpal(), printdir(), vgaon(), vgaoff(), DFsetfind(),
  26. *            DFfind(), DFdup(), DFaccess(), DFputelement(), slidepal(),
  27. *            squishpal(), mousefunc(), updatescrn()
  28. *  Called by    :    options()
  29. **********************************************************************/
  30. void parse(c)
  31. unsigned char c;
  32. {
  33.     switch (c){                /* check for each command possible */
  34.         case 'w':        /* switch preference between expanding and interpolating */
  35.             expandit=!expandit;
  36.             break;
  37.  
  38.         case 'Q':        /* get out of the program entirely */
  39.             getout=1;
  40.             break;
  41.  
  42.         case '/':        /* slow down the animation speed */
  43.             anispeed+=100;
  44.             break;
  45.  
  46.         case '*':        /* speed up the animation speed */
  47.             anispeed-=100;
  48.             if(anispeed<0)
  49.                 anispeed=0;
  50.             break;
  51.  
  52.         case ',':        /* expand a portion of the screen to fill the screen */
  53.             if(mouse && mode==NO9)
  54.                 makecur9(mx,my);
  55.             textmode();
  56.             printf("Type Position Of The Upper Left Hand Corner Of The Square to Magnify:\n");
  57.             printf("Input X-coordinate for upper left hand corner:");
  58.             scanf("%d",&i);
  59.             printf("\n\nInput Y-coordinate for upper left hand corner:");
  60.             scanf("%d",&j);
  61.             printf("\n\nType Position Of The Lower Right Hand Corner Of The Square to Magnify:\n");
  62.             printf("Input X-coordinate for lower right hand corner:");
  63.             scanf("%d",&k);
  64.             printf("\n\nInput Y-coordinate for lower right hand corner:");
  65.             scanf("%d",&l);
  66.             grafmode();                                    /* clear the screen */
  67.             if(expandit) {                /* if preference is to expand the image */
  68.                 if(expand(i+xoff,j+yoff,k+xoff,l+yoff)==1)            /* expand chosen area to fill screen */
  69.                     waitq();    /* wait for keypress */
  70.               }    /* end if */
  71.             else {                        /* if preference is to interpolate the image */
  72.                 if(interpolate(i+xoff,j+yoff,k+xoff,l+yoff)==1)            /* expand and smooth the chosen area to fill the screen */
  73.                     waitq();    /* wait for keypress */
  74.               }    /* end else */
  75.             grafmode();
  76.             updatescrn();
  77.             break;
  78.  
  79.         case '&':        /* bounce ball on screen, undocumented feature */
  80.             bounce();
  81.             break;
  82.  
  83.         case 'p':        /* move the color bar on the screen */
  84.             if(mouse && mode==NO9)
  85.                 makecur9(mx,my);
  86.             textmode();
  87.             printf("Type Position To Move Upper Left Hand Corner Of The Color Bar To:\n");
  88.             printf("Input X-coordinate for upper left hand corner:");
  89.             scanf("%d",&i);
  90.             printf("\n\nInput Y-coordinate for upper left hand corner:");
  91.             scanf("%d",&j);
  92.             if(i<0)    /* move color bar to those coor. */
  93.                 pal_xoff=0;
  94.             else
  95.                 if(i>(maxx-256))
  96.                     pal_xoff=maxx-256;
  97.                 else
  98.                     pal_xoff=i;
  99.             if(j<0)
  100.                 pal_yoff=0;
  101.             else
  102.                 if(j>(maxy-8))
  103.                     pal_yoff=maxy-8;
  104.                 else
  105.                     pal_yoff=j;
  106.             grafmode();
  107.             updatescrn();
  108.             break;
  109.  
  110.         case 'm':        /* attempt to activate the mouse */
  111. #ifdef MOUSE
  112.             if(!mouse) {    /* if the mouse is not activated then do so */
  113.                 if(before) {    /* if the mouse has been activated before, then just show cursor */
  114.                     if(mode==NO9)
  115.                         makecur9(mx,my);
  116.                     else {
  117.                         m1=1;            /* show mouse cursor */
  118.                         mousecl(&m1,&m2,&m3,&m4);
  119.                       }    /* end else */
  120.                     mouse=1;
  121.                   }    /* end if */
  122.                 else {            /* if the mouse has never been activated */
  123.                     m1=0;            /* reset mouse */
  124.                     m2=0;
  125.                     m3=0;
  126.                     m4=0;
  127.                     mousecl(&m1,&m2,&m3,&m4);
  128.                     if(!m1)        /* mouse not there */
  129.                         displayerr("PC Mouse not installed");
  130.                     else {    /* mouse is there */
  131.                         m1=7;                /* set min/max horizontal position */
  132.                         m3=0;
  133.                         switch(mode) {        /* get different sizes for different screens */
  134.                             case NO9:
  135.                                 m4=4095;
  136.                                 break;
  137.  
  138.                             case VGA:
  139.                             case EGA:
  140.                             default:
  141.                                 m4=639;
  142.                                 break;
  143.                           }    /* end switch */
  144.                         mousecl(&m1,&m2,&m3,&m4);
  145.                         m1=8;                /* set min/max vertical position */
  146.                         m3=0;
  147.                         switch(mode) {        /* get different sizes for different screens */
  148.                             case NO9:
  149.                                 m4=4095;
  150.                                 break;
  151.  
  152.                             case VGA:
  153.                                 m4=199;
  154.                                 break;
  155.  
  156.                             case EGA:
  157.                             default:
  158.                                 m4=349;
  159.                                 break;
  160.                           }    /* end switch */
  161.                         mousecl(&m1,&m2,&m3,&m4);
  162.                         m1=4;                /* set mouse cursor position */
  163.                         switch(mode) {        /* get different sizes for different screens */
  164.                             case NO9:
  165.                                 m3=2048;
  166.                                 m4=2048;
  167.                                 break;
  168.  
  169.                             case VGA:
  170.                                 m3=320;
  171.                                 m4=100;
  172.                                 break;
  173.  
  174.                             case EGA:
  175.                             default:
  176.                                 m3=320;
  177.                                 m4=175;
  178.                                 break;
  179.                           }    /* end switch */
  180.                         mousecl(&m1,&m2,&m3,&m4);
  181.                         switch(mode) {        /* decide the inital mouse positions */
  182.                             case VGA:
  183.                                 mx=160;                /* set old mouse positions */
  184.                                 my=100;
  185.                                 break;
  186.  
  187.                             case NO9:
  188.                                 mx=my=256;
  189.                                 break;
  190.  
  191.                             case EGA:
  192.                             default:
  193.                                 mx=160;
  194.                                 my=175;
  195.                                 break;
  196.                           }    /* end switch */
  197.                         mouse=1;
  198.                         before=1;
  199.                         if(mode==NO9)            /* put the mouse cursor on the no9 screen */
  200.                             makecur9(mx,my);
  201.                         else
  202.                             makemouse();        /* routine to make the mouse cursor into a cross hair */
  203.                       }    /* end else if */
  204.                   }    /* end else */
  205.               }    /* end if */
  206.             else {
  207.                 if(mode==NO9)
  208.                     makecur9(mx,my);
  209.                 else {
  210.                     m1=2;        /* function to remove the cursor */
  211.                     mousecl(&m1,&m2,&m3,&m4);
  212.                   }    /* end if */
  213.                 mouse=0;    /* deactivate the mouse */
  214.               }    /* end else */
  215. #endif
  216.             break;
  217.     
  218.         case 'k':        /* end animation mode */
  219.             animate=0;
  220.             break;
  221.  
  222.         case 'i':        /* show information about screen */
  223.             if(mouse && mode==NO9)
  224.                 makecur9(mx,my);
  225.             textmode();
  226.             printf("Information Screen\n\n");
  227.             printf("Coor. Of Upper Left Hand Corner = %d,%d\n\n",xoff,yoff);
  228.             printf("X & Y Dimensions of the Image = %d,%d\n\n",xdim,ydim);
  229.             if(expandit)
  230.                 printf("Expand Image Preference Set\n\n");
  231.             else
  232.                 printf("Interpolate Image Preference Set\n\n");
  233.             if(animate)
  234.                 printf("Animation Mode Active\n\n");
  235.             else
  236.                 printf("Animation Mode Not Active\n\n");
  237.             if(mouse)
  238.                 printf("Mouse Activated\n\n");
  239.             else
  240.                 printf("Mouse Not Activated\n\n");
  241.             printf("Hit <enter> To Continue\n");
  242.             waitq();    /* wait for keypress */
  243.             grafmode();
  244.             updatescrn();
  245.             break;
  246.  
  247.         case 'h':        /* enter fiddle mode */
  248.             c=' ';
  249.             if (kbhit()) {
  250.                 c=getch();
  251.                 if(c==0)        /* check for extended character code */
  252.                     c=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  253.               }    /* end if */
  254.             offset=0;
  255.             slope=10;
  256.             while(c!=13) {
  257.                 switch(c) {        /* check for proper fiddle commands */
  258.                     case '?':        /* help on fiddle commands */
  259.                         if(mouse && mode==NO9)
  260.                             makecur9(mx,my);
  261.                         textmode();
  262.                         printf("Fiddle Help Screen\n\n");
  263.                         printf("<left arrow>\t - Shift Palette To The Left\n");
  264.                         printf("<right arrow>\t - Shift Palette To The Right\n");
  265.                         printf("<up arrow>\t - Compress Palette\n");
  266.                         printf("<down arrow>\t - Expand Palette\n");
  267.                         printf("<enter>\t\t - Exit Fiddle Mode\n");
  268.                         printf("?\t\t - Show Help Screen\n\n");
  269.                         printf("Hit <enter> To Resume Fiddling\n");
  270.                         waitq();    /* wait for keypress */
  271.                         grafmode();
  272.                         updatescrn();
  273.                         break;
  274.  
  275.                     case SCRLLEFT:        /* shift palette left */
  276.                         offset--;
  277.                         squishpal(slope,offset);
  278.                         putpal();
  279.                         break;
  280.  
  281.                     case SCRLUP:        /* compress palette together */
  282.                         slope++;
  283.                         squishpal(slope,offset);
  284.                         putpal();
  285.                         break;
  286.  
  287.                     case SCRLDOWN:        /* expand palette */
  288.                         slope--;
  289.                         squishpal(slope,offset);
  290.                         putpal();
  291.                         break;
  292.  
  293.                     case SCRLRIGHT:        /* shift palette right */
  294.                         offset++;
  295.                         squishpal(slope,offset);
  296.                         putpal();
  297.                         break;
  298.                   } /* end switch */
  299.                 c=getch();        /* get the next command */
  300.                 if(c==0)
  301.                     c=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  302.               } /* end while */
  303.             break;
  304.  
  305.         case 'g':        /* invert the palette bitwise */
  306.             for(j=0; j<256; j++) {
  307.                 rmap[j]=~(int)rmap[j];
  308.                 gmap[j]=~(int)gmap[j];
  309.                 bmap[j]=~(int)bmap[j];
  310.               }    /* end for j */
  311.             putpal();
  312.             break;
  313.  
  314.         case 't':        /* transpose palette */
  315.             for(j=0; j<128; j++) {
  316.                 swap(&rmap[j],&rmap[255-j]);
  317.                 swap(&gmap[j],&gmap[255-j]);
  318.                 swap(&bmap[j],&bmap[255-j]);
  319.               }    /* end for j */
  320.             putpal();
  321.             break;
  322.  
  323.         case 'd':        /* make the current pallete into the default palette */
  324.             if(mode!=EGA) {
  325.                 memcpy(rpal[palnum],rmap,256);
  326.                 memcpy(gpal[palnum],gmap,256);
  327.                 memcpy(bpal[palnum],bmap,256);
  328.               } /* end if */
  329.             else 
  330.                 memcpy(egapals[palnum],regrs,16);
  331.             break;
  332.  
  333.         case '+':        /* speed up the palettes rotation */
  334.             if(mode!=EGA)
  335.                 palspeed+=100;
  336.             else
  337.                 palspeed+=20;
  338.             break;
  339.  
  340.         case '-':        /* slow down the palettes rotation */
  341.             if(mode!=EGA)
  342.                 palspeed-=100;
  343.             else
  344.                 palspeed-=20;
  345.             if(palspeed<0)
  346.                 palspeed=0;
  347.             break;
  348.  
  349.         case 'u':        /* swap the red, green, and blue palettes */
  350.             memcpy(trans1,rmap,256);
  351.             memcpy(rmap,gmap,256);
  352.             memcpy(gmap,bmap,256);
  353.             memcpy(bmap,trans1,256);
  354.             putpal();
  355.             break;
  356.  
  357.         case 's':        /* save palette to disk */
  358.             if(filetype=='h') { /* save the HDF palette */
  359.                 DFsetfind(dff, DFTG_IP8, DFREF_WILDCARD);    /* set the HDF file to a palette */
  360.                 while(!DFfind( dff, &ddstr)){ /* step through all the palettes */
  361.                     lasttag=ddstr.tag;
  362.                     lastref=ddstr.ref;
  363.                   } /* end while */
  364.                 newref=lastref+1;        /* make a new reference number */
  365.                 if(DFdup(dff,imtag,newref,imtag,imref))        /* try to make a new palette space */
  366.                     displayerr("Error On DFdup");
  367.                 else { /* ok DFdup call */
  368.                     for(i=0; i<256; i++) {    /* put individual palettes into the big one */
  369.                         pal[i*3]=rmap[i];
  370.                         pal[i*3+1]=gmap[i];
  371.                         pal[i*3+2]=bmap[i];
  372.                       } /* end for */
  373.                     i=DFaccess( dff, DFTG_IP8, newref, "w");    /* tell HDF to start writing */
  374.                     if(i<0) {
  375.                         printf("return code from DFaccess = %d,%d\n",i, DFerror);
  376.                         i=getchar();
  377.                       } /* end if */
  378.                     if((i=DFputelement(dff,DFTG_IP8,newref,pal,(long) 768))<=0)
  379.                         displayerr("Error On DFputelement In Save Palette Subroutine");
  380.                     else
  381.                         DFupdate(dff);
  382.                     DFaccess( dff, lasttag, newref, "r");    /* tell HDF to start reading */
  383.                   } /* end else */
  384.               }    /* end if */
  385.             else{        /* save a binary palette */
  386.                 if(mouse && mode==NO9)
  387.                     makecur9(mx,my);
  388.                 textmode();
  389.                 printf("Input Name Of Palette File (or <return> to exit) ");
  390.                 readstr(&palfile[0]);
  391.                 printf("\n");
  392.                 if(*palfile!=0) {    /* if its not a null name attempt to write out file */
  393.                     if (NULL == (pfp = fopen(palfile,"wb"))) {    /* open the palette file */
  394.                         printf("Error on palette file open\n");
  395.                         printf("Hit Any Key To Continue\n");
  396.                         waitq();
  397.                       } /* end if */
  398.                     else {    /* write out the palette */
  399.                         fwrite(rmap,1,256,pfp);
  400.                         fwrite(gmap,1,256,pfp);
  401.                         fwrite(bmap,1,256,pfp);
  402.                         fclose(pfp);
  403.                       } /* end else */
  404.                   }    /* end if */
  405.                 grafmode();
  406.                 updatescrn();
  407.               }    /* end else */
  408.             break;
  409.  
  410.         case 'l':        /* load in a new palette */
  411.             if(filetype=='h') {    /* load in a new HDF palette */
  412.                 findpal();
  413.                 putpal();
  414.               } /* end if */
  415.             else {    /* load in a new binary palette */
  416.                 if(mouse && mode==NO9)
  417.                     makecur9(mx,my);
  418.                 textmode();
  419.                 *palfile='?';
  420.                 while(*palfile=='?') {
  421.                     printf("Input Name Of Palette File (or ? to display palettes): ");
  422.                     readstr(&palfile[0]);
  423.                     printf("\n");
  424.                     if(*palfile=='?')    /* print directory of palettes */
  425.                         printdir();
  426.                     else    /* just load in the palette */
  427.                         if(*palfile!=0)
  428.                             newpal(palfile);
  429.                   } /* end while */
  430.                 grafmode();
  431.                 updatescrn();
  432.               } /* end else */
  433.             break;
  434.  
  435.         case 'x':        /* get x,y coor. for upper left corner */
  436.             if(mouse && mode==NO9)
  437.                 makecur9(mx,my);
  438.             textmode();
  439.             printf("Input X-coordinates for upper left hand corner:");
  440.             scanf("%d",&i);
  441.             printf("\n\nInput Y-coordinates for upper left hand corner:");
  442.             scanf("%d",&j);
  443.             if((i<0) || ((xdim-maxx)<0))    /* move image to those coor. */
  444.                 xoff=0;
  445.             else
  446.                 if(i>(xdim-maxx))
  447.                     xoff=xdim-maxx;
  448.                 else
  449.                     xoff=i;
  450.             if((j<0) || ((ydim-maxy)<0))
  451.                 yoff=0;
  452.             else
  453.                 if(j>(ydim-maxy))
  454.                     yoff=ydim-maxy;
  455.                 else
  456.                     yoff=j;
  457.             grafmode();
  458.             updatescrn();
  459.             break;
  460.  
  461.         case SCRLLEFT:        /* move screen left ten pixels */
  462.             scroll('l');
  463.             break;
  464.  
  465.         case SCRLUP:        /* move screen up ten pixels */
  466.             scroll('u');
  467.             break;
  468.  
  469.         case SCRLDOWN:        /* move screen down ten pixels */
  470.             scroll('d');
  471.             break;
  472.  
  473.         case SCRLRIGHT:        /* move screen right ten pixels */
  474.             scroll('r');
  475.             break;
  476.  
  477.         case PAGEUP:        /* move screen up a full screen */
  478.             scroll('U');
  479.             break;
  480.  
  481.         case PAGEDOWN:        /* move screen down a full screen */
  482.             scroll('D');
  483.             break;
  484.  
  485.         case HOME:        /* move screen to the upper left hand corner */
  486.             scroll('H');
  487.             break;
  488.  
  489.         case END:        /* move screen to lower left hand corner */
  490.             scroll('E');
  491.             break;
  492.  
  493.         case 'c':        /* show color bar on the screen */
  494.             if(!show)
  495.                 showpal();
  496.             else
  497.                 nopal(xoff,yoff);
  498.             show=!show;
  499.             break;
  500.  
  501.         case 'n':         /* change current palette */
  502.             palnum++;
  503.             if (palnum>palmax)
  504.                 palnum=0;
  505.             if(mode!=EGA) {
  506.                 memcpy(rmap,rpal[palnum],256);
  507.                 memcpy(gmap,gpal[palnum],256);
  508.                 memcpy(bmap,bpal[palnum],256);
  509.               } /* end if */
  510.             else
  511.                 memcpy(regrs,egapals[palnum],16);
  512.             putpal();
  513.              break;
  514.  
  515.         case 'o':    /* turn vga screen off or on */
  516.             if(mode==VGA)
  517.                 if(screen) {
  518.                     vgaoff();
  519.                     screen=0;
  520.                   }    /* end if */
  521.                 else {
  522.                     vgaon();
  523.                     screen=1;
  524.                   }    /* end else if */
  525.             break;
  526.  
  527.         case 'f':        /* rotate palette forwards continuously */
  528.             l='z';
  529.             while (l!=' ') {
  530.                 if(mode!=EGA) {
  531.                     i=rmap[0];
  532.                     j=gmap[0];
  533.                     k=bmap[0];
  534.                     memcpy(rmap,rmap+1,255);    /* shift palettes down one byte */
  535.                     memcpy(gmap,gmap+1,255);
  536.                     memcpy(bmap,bmap+1,255);
  537.                     rmap[255]=i;
  538.                     gmap[255]=j;
  539.                     bmap[255]=k;
  540.                     j=palspeed;
  541.                   } /* end if */
  542.                 else {
  543.                     i=regrs[0];
  544.                     for(j=0; j<=14; j++)
  545.                         regrs[j]=regrs[j+1];
  546.                     regrs[15]=i;
  547.                     j=palspeed+2000;
  548.                   } /* end else */
  549.                 putpal();
  550.                 if(kbhit()) {
  551.                     l=getch();
  552.                     if(l==0)                /* get extended code if necessary */
  553.                         l=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  554.                   }    /* end if */
  555.                 if(l=='+') {        /* increase the palettes rotation speed */
  556.                     if(mode!=EGA)
  557.                         palspeed+=100;
  558.                     else
  559.                         palspeed+=20;
  560.                     l='x';
  561.                   }    /* end if */
  562.                 if(l=='-') {        /* decrease palette rotation speed */
  563.                     if(mode!=EGA)
  564.                         palspeed-=100;
  565.                     else
  566.                         palspeed-=20;
  567.                     l='x';
  568.                   }    /* end if */
  569.                 for(; j<=2500; j++); 
  570.               } /* end while */
  571.               break;
  572.  
  573.         case 'b':        /* rotate palette backwards continuously */
  574.             l='x';
  575.             while (l!=' ') {
  576.                 if(mode!=EGA) {
  577.                     i=rmap[255];
  578.                     j=gmap[255];
  579.                     k=bmap[255];
  580.                     memcpy(rmap+1,rmap,255);        /* shift palettes up one byte */
  581.                     memcpy(gmap+1,gmap,255);
  582.                     memcpy(bmap+1,bmap,255);
  583.                     rmap[0]=i;
  584.                     gmap[0]=j;
  585.                     bmap[0]=k;
  586.                     j=palspeed;
  587.                   } /* end if */
  588.                 else {
  589.                     i=regrs[15];
  590.                     for(j=15; j>=1; j--)
  591.                         regrs[j]=regrs[j-1];
  592.                     regrs[0]=i;
  593.                     j=palspeed+2000;
  594.                   } /* end else */
  595.                 putpal();
  596.                 if(kbhit()) {
  597.                     l=getch();
  598.                     if(l==0)                /* get extended code if necessary */
  599.                         l=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  600.                   }    /* end if */
  601.                 if(l=='+') {        /* increase the palettes rotation speed */
  602.                     if(mode!=EGA)
  603.                         palspeed+=100;
  604.                     else
  605.                         palspeed+=20;
  606.                     l='x';
  607.                   }    /* end if */
  608.                 if(l=='-') {        /* decrease palette rotation speed */
  609.                     if(mode!=EGA)
  610.                         palspeed-=100;
  611.                     else
  612.                         palspeed-=20;
  613.                     l='x';
  614.                   } /* end if */
  615.                 for(; j<=2500; j++);
  616.               } /* end while */
  617.               break;
  618.  
  619.         case 'e':        /* rotate palette forwards once */
  620.             rotate('r');
  621.               break;
  622.  
  623.         case 'v':        /* rotate palette backwards once */
  624.             rotate('l');
  625.             break;
  626.  
  627.         case 'r':    /* reset the palette to its initial values */
  628.             if(mode!=EGA) {
  629.                 memcpy(rmap,rpal[palnum],256);
  630.                 memcpy(gmap,gpal[palnum],256);
  631.                 memcpy(bmap,bpal[palnum],256);
  632.               } /* end if */
  633.             else 
  634.                 memcpy(regrs,egapals[palnum],16);
  635.             palspeed=0;    /* set the palette rotation speed back to zero also */
  636.             putpal();
  637.             break;
  638.       } /* end switch */
  639. }    /* end parse() */
  640.